/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.models.impl;

import com.ntb.entities.Payments;
import com.ntb.common.DatabaseConnection;
import com.ntb.models.PaymentsDAO;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author DATBP
 */
public class PaymentsDAOImpl extends DatabaseConnection implements PaymentsDAO {

    public Iterator<Payments> getList(int saleID) {
        try {
            ArrayList arr = new ArrayList();
            openConnection();
            ResultSet rs = getResultset("SELECT * FROM Payments WHERE SaleID = " + saleID + " ORDER BY CreatedDate DESC");
            while (rs.next()) {
                Payments payment = new Payments();
                payment.setPayID(rs.getInt("PayID"));
                payment.setSaleID(rs.getInt("SaleID"));
                payment.setCreatedDate(rs.getDate("CreatedDate"));
                payment.setAmountDue(rs.getInt("AmountDue"));
                payment.setDueDate(rs.getDate("DueDate"));
                payment.setAmountPaid(rs.getInt("AmountPaid"));
                arr.add(payment);
            }
            closeConnection();
            if (arr.size() > 0) {
                return arr.iterator();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public Payments getRow(int id) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Select * From Payments Where PayID=?");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            Payments row = new Payments();
            if (rs.next()) {
                row.setPayID(rs.getInt("PayID"));
                row.setSaleID(rs.getInt("SaleID"));
                row.setCreatedDate(rs.getDate("CreatedDate"));
                row.setAmountDue(rs.getInt("AmountDue"));
                row.setDueDate(rs.getDate("DueDate"));
                row.setAmountPaid(rs.getInt("AmountPaid"));
                closeConnection();
                return row;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public int add(Payments object) {
        try {
            openConnection();
            String sql = "{call spu_addPayment(?, ?, ?)}";
            CallableStatement cst = conn.prepareCall(sql);
            cst.setInt(1, object.getSaleID());
            cst.setDate(2, object.getDueDate());
            cst.setInt(3, object.getAmountPaid());
            int rs = cst.executeUpdate();
            closeConnection();
            return rs;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public int update(Payments object) {
        try {
            openConnection();
            String sql = "{call spu_updatePayment(?, ?, ?, ?)}";
            CallableStatement cst = conn.prepareCall(sql);
            cst.setInt(1, object.getSaleID());
            cst.setInt(2, object.getPayID());
            cst.setDate(3, object.getDueDate());
            cst.setInt(4, object.getAmountPaid());
            int rs = cst.executeUpdate();
            closeConnection();
            return rs;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }

    public int delete(int id) {
        try {
            openConnection();
            PreparedStatement pst = conn.prepareStatement("Delete Payments Where PayID=?");
            pst.setInt(1, id);
            int result = pst.executeUpdate();
            closeConnection();
            return result;
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return -1;
    }
}
